package com.usian.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.usian.Feign;
import com.usian.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RequestMapping("/consumer")
@RestController
@RefreshScope//更新
public class ConController {
    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private Feign feign;

    @Value("server.port")
    private String demo;

    /*@GetMapping("/findById")
    public User finById(Integer id) {
        //1找注册中心获取服务实例
        ServiceInstance instance = loadBalancerClient.choose("provider");
        //2找到实例拼接访问地址
        String url="Http://"+instance.getHost()+":"+instance.getPort()+"/user/findById/"+id;
        //3发起调用
        User user = restTemplate.getForObject(url, User.class);
        System.out.println(demo);
        return user;
    }*/

    @GetMapping("/findById/{id}")
    @SentinelResource(value = "findById",blockHandler = "goushi",fallback = "baba")
    public User finById(@PathVariable  Integer id) {
        int i=1/0;
        User user = feign.findById(id);
        return user;
    }

    public User goushi(@PathVariable  Integer id, BlockException e){
        return new User(id,"王轲轲别白嫖了该付费了",12);
    }

    public User baba(@PathVariable  Integer id){
        return new User(id,"杨龙权别白嫖了该付费了",12);
    }

    @GetMapping("/findByIdd")
    public User findByIdd(Integer id) {
        User user = feign.findById(id);
        return user;
    }

    @PostMapping("/findByUser")
    public User findByUser(@RequestBody User user){
        System.out.println(user.toString());
        return feign.findById(user.getId());
    }
}
